// pages/welfareMall/cart/order/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar,   //状态栏高度
    CustomBar: app.globalData.CustomBar,  //头部nav高度
    backIcon: true,   // 返回按钮是否显示
    title: "填写订单",  //页面标题
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    cartList: [],
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    totalPrice: 0,
    currentPage: 1,
    pageSize: 8,
    totalYD: 0,
    payVisible: false,
    consignee: '',
    detailAddress: '',
    area: '',
    timer: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.idArr)
    this.setData({
      shopcartList: options.idArr.split(",")
    })
    this.getList()
  },
  getList() {
    const that = this
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "currentPage": this.data.currentPage,
      "pageSize": this.data.pageSize,
      "prizeOrderList": this.data.shopcartList
    }
    // console.log(data)
    wx.request({
      url: app.globalData.baseUrl + 'prizeorder/goPrizePayInfo.do',
      data: data,
      method: 'post',
      success(res) {
        console.log(res)
        that.setData({
          cartList: res.data.productList,
          totalPrice: res.data.totalPrice,
          totalYD: res.data.totalYD
        })
      }
    })
  },
  pay() {
    const that = this
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "consignee": this.data.consignee,
      sendMode: "唐滔",
      "mobileNo": this.data.phone * 1,
      "area": this.data.region.join(''),
      "detailAddress": this.data.detailAddress,
      "prizeOrderList": this.data.shopcartList
    }
    // console.log(data)

    // 如果没有填写订单提示用户啊
    const consignee = this.data.consignee;
    const mobileNo = this.data.phone;
    const detailAddress = this.data.detailAddress;
    if (consignee !== '' && mobileNo !== '' && detailAddress !== '') {
      wx.request({
        url: app.globalData.baseUrl + 'prizeorder/goPrizePay.do',
        method: 'post',
        data: data,
        success(res) {
          console.log(res)
          if (res.data.isSuccess) {
            wx.showToast({
              title: '下单成功',
              success() {
                var timer = null
                timer = setTimeout(function() {
                  that.updateCartNum()
                  clearTimeout(timer)
                }, 3000)
                  
              }
            })
          }
          if (res.data.errorMsg == "奖品库存不足!") {
            wx.showToast({
              title: res.data.errorMsg,
              image: '/assets/images/icon/error.png'
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请填写订单信息',
        image: '/assets/images/icon/error.png'
      })
    }
  },
  updateCartNum() {
    var pages = getCurrentPages();//当前页面栈
    var mallIndex = pages[0]
    
    mallIndex.getCartMes()
    wx.navigateBack({
      delta: pages.length
    })
  },
  detailAddressChange(e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  phoneChange(e) {
    var val = e.detail.value,
          that = this
    clearTimeout(that.timer)
    that.timer = setTimeout(function() {
      that.setData({
        phone: val
      })
      var rexp = /^1[34578]\d{9}$/
      if (!rexp.test(val)){
        wx.showToast({
          title: '请输入正确的手机号',
        })
      }
    }, 1000)
  },
  consigneeChange(e) {
    this.setData({
      consignee: e.detail.value
    })
  },
  showPay() {
    const consignee = this.data.consignee;
    const mobileNo = this.data.phone;
    const detailAddress = this.data.detailAddress;
    if (consignee !== '' && mobileNo !== '' && detailAddress !== '') {
      this.setData({
        payVisible: true
      })
    }else{
      wx.showToast({
        title: '请填写订单信息',
        image: '/assets/images/icon/error.png'
      })
    }
    
  },
  hidePay() {
    this.setData({
      payVisible: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //省市区选择函数
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})